#include "LCD_Ascii.h"

inline static UIN64 Get_Ascii(enum AsciiList LetterID);

static void Printline_Ascii(UIN64 * LetterGraph, UIN16 Index, UIN16 LineLoop, struct LCD_Letter_64 * Letter);

UIN16 Digits_2[5] = {0};

void LCD_PrintLetter(struct LCD_Letter_64 * Letter)
{

	UIN16 index = ZERO_16;
	UIN16 LoopIndex = ZERO_16;
	UIN64 Letter_Graph = Get_Ascii(Letter->LetterID);
	UIN16 Size = (Letter->Height) * (Letter->Width);

	struct LCD_Coord Temp_Obj;

	Temp_Obj.Height = (Letter->Height) * ((UIN16) Letter->Scale);
	Temp_Obj.Width	= (Letter->Width) * ((UIN16) Letter->Scale);
	Temp_Obj.X_Add	= Letter->X_Add;
	Temp_Obj.Y_Add	= Letter->Y_Add;



	LCD_SetWindow(&Temp_Obj);
	LCD_SetX(Letter->X_Add);
	LCD_SetY(Letter->Y_Add);

	for(index = ZERO_16; index < Size; (index +=(Letter->Width)) )
	{
		for(LoopIndex = ZERO_16; LoopIndex < (Letter->Scale); LoopIndex++)
		{
			Printline_Ascii(&Letter_Graph,index,LoopIndex, Letter);
		}
	}
	LCD_CloseWindow();
	return;

}

void LCD_PrintNum16(UIN16 * P_NUM, UIN16 X_Cor, UIN16 Y_Cor)
{
	UIN8 i;
	UIN16 Digits[5] = {0};
	UIN8 Begin = ZERO_8;

	SplitDecDigits16(*P_NUM,Digits);

	for(i = ZERO_8; i < 5; i++)
	{
		Digits_2[i] = Digits[i];
	}
	struct LCD_Letter_64 NUM;

	NUM.Colour = BLACK;
	NUM.Height = (UIN16) 8u;
	NUM.Width = (UIN16) 8u;
	NUM.Scale = ONE_8;
	NUM.LetterID = Nu_0;
	NUM.X_Add = X_Cor;
	NUM.Y_Add = Y_Cor;


	for(i = ZERO_8; i < 5; i++)
	{

		if(Digits[FIVE_8 - i-1] > ZERO_8)
		{
			Begin = ONE_8;
		}
		else
		{

		}


		if(Begin == ONE_8)
		{
			NUM.LetterID = Nu_0 + Digits[FIVE_8 - i-1];
			if (NUM.X_Add > (Max_Col-NUM.Width))
			{
				NUM.X_Add = ZERO_16;
				NUM.Y_Add += NUM.Height;
			}
			else
			{

			}

			LCD_PrintLetter(&NUM);

			NUM.X_Add += NUM.Width;
		}
	}

	return;
}

inline static UIN64 Get_Ascii(enum AsciiList LetterID)
{
	UIN64 Temp = ZERO_64;
	switch(LetterID)
	{
		case A:
			Temp = Letter_A;
			break;
		case B:
			Temp = Letter_B;
			break;
		case Nu_0:
			Temp = Num_0;
			break;
		case Nu_1:
			Temp = Num_1;
			break;
		case Nu_2:
			Temp = Num_2;
			break;
		case Nu_3:
			Temp = Num_3;
			break;
		case Nu_4:
			Temp = Num_4;
			break;
		case Nu_5:
			Temp = Num_5;
			break;
		case Nu_6:
			Temp = Num_6;
			break;
		case Nu_7:
			Temp = Num_7;
			break;
		case Nu_8:
			Temp = Num_8;
			break;
		case Nu_9:
			Temp = Num_9;
			break;
		default:
			break;
	}
	return Temp;
}

static void Printline_Ascii(UIN64 * LetterGraph, UIN16 Index, UIN16 LineLoop, struct LCD_Letter_64 * Letter)
{
	UIN16 EndOfLine = ZERO_16;

	UIN64 Curr	= ZERO_64;

	UIN8 Loop = ZERO_8;

	UIN16 X_Add = ZERO_16;

	UIN16 Y_Add = ZERO_16;



	EndOfLine = Index + Letter->Width;

	for(Curr = Index; Curr < EndOfLine; Curr++)
	{

		if(CheckBit((*LetterGraph), Curr) > ZERO_64)
		{
			for(Loop = ZERO_8; Loop < (Letter->Scale); Loop++)
			{
				LCD_WritePixel(Letter->Colour);
			}
		}
		else
		{

			X_Add = (Letter->X_Add)  + ((Curr+ONE_64) REMAIN_BY (Letter->Width))*((UIN16) (Letter->Scale));
			LCD_SetX(X_Add);

			Y_Add = (Letter->Y_Add) + ((Curr+ONE_64) DEVIDE_BY (Letter->Width))  *((UIN16) (Letter->Scale)) + LineLoop;
			LCD_SetY(Y_Add);
		}
	}

	return;
}
